package com.hx.vending.service.impl;

import com.hx.vending.bean.ModelTrails;
import com.hx.vending.bean.Rise;
import com.hx.vending.bean.Trails;
import com.hx.vending.dao.ModelTrailsMapper;
import com.hx.vending.dao.RiseMapper;
import com.hx.vending.dao.TrailsMapper;
import com.hx.vending.service.IRiseService;
import com.hx.vending.util.Msg;
import com.hx.vending.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator |on 2018/11/28.
 * 层
 */
@Service
public class RiseServiceImpl implements IRiseService{

    @Autowired
    private RiseMapper riseMapper;

    @Autowired
    private TrailsMapper trailsMapper;

    @Autowired
    private ModelTrailsMapper modelTrailsMapper;



    /**
     * 添加或修改层信息
     * @param rise
     * @return
     */
    public Msg updateOrInsertRise(Rise rise){
        if(ToolUtil.isNotEmpty(rise)){

            if (ToolUtil.isNotEmpty(rise.getRiseId())) {
                //层id不为空，修改层信息
                int updateRise = this.riseMapper.updateByPrimaryKeySelective(rise);
                if (updateRise != 0 && ToolUtil.isNotEmpty(rise.getTrailsList())) {

                    //还有删除货道的情况
                    //将该层下的货道查出来
                    ModelTrails modelTrails = new ModelTrails();
                    modelTrails.setModelTrailsModelId(rise.getRiseModelId());
                    modelTrails.setModelTrailsRiseId(rise.getRiseId());
                    List<ModelTrails> modelTrailsList = this.modelTrailsMapper.selectByExampleSelective(modelTrails);
                    //所有货道id   a,b,c,
                    String trailsId = null;

                    for (ModelTrails forModelTrails: modelTrailsList) {
                        if (trailsId == null){
                            trailsId = forModelTrails.getModelTrailsTrailsId().toString()+",";
                        }else {
                            trailsId = trailsId + forModelTrails.getModelTrailsTrailsId().toString()+",";
                        }
                    }


                    List<Trails> trailsList = rise.getTrailsList();
                    for (Trails trails: trailsList) {
                        if (ToolUtil.isNotEmpty(trails.getTrailsId())) {
                            //货道id不为空
                            //修改货道信息
                            this.trailsMapper.updateByPrimaryKeySelective(trails);

                            //该货到依然存在
                            trailsId.replace(trails.getTrailsId().toString()+",", "");
                        }else {
                            //货道id为空
                            //添加货道信息
                            trails.setTrailsRiseId(rise.getRiseId());
                            this.trailsMapper.insertSelective(trails);
                        }
                    }

                    //删除不存在的货道
                    if (ToolUtil.isNotEmpty(trailsId)) {

                        trailsId = trailsId.substring(0, trailsId.length()-1);
                        String [] deleteTrailsId = trailsId.split(",");
                        for (String id: deleteTrailsId) {
                            this.trailsMapper.deleteByPrimaryKey(Integer.parseInt(id));
                            this.modelTrailsMapper.deleteByTrailsId(Integer.parseInt(id));
                        }
                    }
                }

            }else {
                //层id为空，添加层信息
                int insertRise = this.riseMapper.insertSelective(rise);
                if (insertRise != 0 && ToolUtil.isNotEmpty(rise.getTrailsList())) {

                    //查出刚添加的层信息
                    Rise selectRise = this.riseMapper.selectByPrimaryKeySelective(rise);

                    List<Trails> trailsList = rise.getTrailsList();
                    for (Trails trails: trailsList) {

                        //层id
                        trails.setTrailsRiseId(selectRise.getRiseId());
                        //添加货道信息
                        int insertTrails = this.trailsMapper.insertSelective(trails);
                        if (insertTrails != 0) {
                            //查询添加的货道信息
                            Trails selectTrails = this.trailsMapper.selectByPrimaryKeySelective(trails);
                            //添加模板关联信息
                            ModelTrails modelTrails = new ModelTrails();
                            //模板id
                            modelTrails.setModelTrailsModelId(rise.getRiseModelId());
                            //层id
                            modelTrails.setModelTrailsRiseId(selectRise.getRiseId());
                            //货道信息
                            modelTrails.setModelTrailsTrailsId(selectTrails.getTrailsId());
                            //添加信息
                            this.modelTrailsMapper.insertSelective(modelTrails);
                        }
                    }

                }

            }

            return Msg.success();
        }else{
            return Msg.fail();
        }
    }


    /**
     * 删除层信息
     * @param riseId
     * @return
     */
    public Msg deleteRise(Integer modelId, Integer riseId){

        if (ToolUtil.isNotEmpty(riseId)) {
            //删除该层货道信息
            int deleteTrails = this.trailsMapper.deleteByRiseId(riseId);
            if (deleteTrails != 0) {
                //删除层信息
                this.riseMapper.deleteByPrimaryKey(riseId);
                //删除模板关联信息
                this.modelTrailsMapper.deleteByRiseIdAndModelId(modelId, riseId);
            }
            return Msg.success();
        }else {
            return Msg.fail();
        }
    }

    @Override
    public int deleteRiseByEquipmentId(String equipmentId) {
        return riseMapper.deleteRiseByEquipmentId(equipmentId);
    }

}
